package com.atguigu.interview.algo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author : Jungle
 * @Description :
 */
public class LruCacheDemo2 {
    /**
     * map查找，构建一个虚拟的双向链表，里面安装的就是一个个node节点，作为数据载体\
     * <p>
     * 1、构造一个node节点 作为数据的载体
     */
    class Node<K, V> {
        K key;
        V value;
        //有前指针和后指针是双向链表
        Node<K, V> next;
        Node<K, V> prev;

        /**
         * 1、无参构造方法
         * 2、带参的构造方法
         */
        public Node() {
            /**
             * 初始化
             */
            prev = next = null;
        }

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
            prev = next = null;
        }
    }

    /**
     * 2、构造一个虚拟的双向链表，里面安放的是我们的Node
     */
    class DoubleLinkedList<K, V> {
        Node<K, V> head;
        Node<K, V> tail;

        //    2.1构造方法
        public DoubleLinkedList() {
            head = new Node<>();
            tail = new Node<>();
            /**
             * 1、头的下一个是尾结点，尾的上一个是头节点
             */
            head.next = tail;
            tail.prev = head;
        }

        //    2.2 添加到头
        public void addHead(Node<K, V> node) {
            node.next = head.next;
            node.prev = head;
            head.next.prev = node;
            head.next = node;
        }

        //2.3 删除节点
        public void removeNode(Node<K, V> node) {
            node.next.prev = node.prev;
            node.prev.next = node.next;
            node.prev = null;
            node.next = null;
        }


        //2.4 获取到最后一个节点
        public Node<K,V> getLast() {
            return tail.prev;
        }

    }

    private int cacheSize;
    Map<Integer, Node<Integer, Integer>> map;
    DoubleLinkedList<Integer, Integer> doubleLinkedList;

    public LruCacheDemo2(int cacheSize) {
        //坑位
        this.cacheSize = cacheSize;
        //查找
        map = new HashMap<>();
        doubleLinkedList = new DoubleLinkedList<>();

        this.cacheSize = cacheSize;
        map = new HashMap<>();
        doubleLinkedList = new DoubleLinkedList<>();
    }

    /**
     * 1、 存在于缓存中，则返回关键字的值，否则返回 -1
     */

    public int get(int key) {
        if (!map.containsKey(key)) {
            return -1;
        }
        Node<Integer, Integer> node = map.get(key);
        doubleLinkedList.removeNode(node);
        doubleLinkedList.addHead(node);
        return node.value;
    }

    /**
     saveOrUpdate method
     */
    public void put(int key,int value){
        //update
        if (map.containsKey(key)) {
            Node<Integer, Integer> node = map.get(key);
            node.value = value;
            map.put(key, node);
            doubleLinkedList.removeNode(node);
            doubleLinkedList.addHead(node);
        }else {
        //    坑位满了
            if (map.size()==cacheSize) {  //坑位满了
                Node<Integer, Integer> lastNode = doubleLinkedList.getLast();
                map.remove(lastNode.key);
                doubleLinkedList.removeNode(lastNode);

            }
        //   才是新增
            Node<Integer, Integer> newNode = new Node<>(key, value);
            map.put(key, newNode);
            doubleLinkedList.addHead(newNode);
        }
    }

    public static void main(String[] args) {
        LruCacheDemo2 lruCacheDemo = new LruCacheDemo2(3);
        lruCacheDemo.put(1, 1);
        lruCacheDemo.put(2, 2);
        lruCacheDemo.put(3, 3);
        System.out.println(lruCacheDemo.map.keySet());
        lruCacheDemo.put(4, 4);
        System.out.println(lruCacheDemo.map.keySet());
        lruCacheDemo.put(3, 3);
        System.out.println(lruCacheDemo.map.keySet());
        lruCacheDemo.put(3, 3);
        System.out.println(lruCacheDemo.map.keySet());
        lruCacheDemo.put(3, 3);
        System.out.println(lruCacheDemo.map.keySet());
        lruCacheDemo.put(5, 5);
        System.out.println(lruCacheDemo.map.keySet());
    }
}
